package com.lenovo.nova.util.graphics;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.Point;
import android.view.MotionEvent;
import android.view.View;

import com.lenovo.nova.util.DevicesUtil;


public class CanvasUtil {

	private static CanvasUtil INSTANCE = new CanvasUtil();
	private Context mContext;

	//y坐标轴开始位置
	private int baseYStart = 500;
	
	//x坐标轴开始位置
	private int baseXStart = 400;
	
	private int maxMarginForY = 0;
	public static CanvasUtil getInstance(Context context) {
		INSTANCE.mContext = context;
		return INSTANCE;
	}

	private Paint paint;
	
	private CanvasUtil(){
		paint = new Paint();
		paint.setTextAlign( Align.LEFT);
	}
	
	
	public void onTouchEvent(View view,MotionEvent event){
		int x = (int) event.getX();
		int y = (int) event.getY();
		
		if(valueofline(new Point(baseYStart, baseXStart), new Point(x, y)) < 200){
			baseYStart = (int) x;
			baseXStart = (int) y;
			view.invalidate();
		}
	
	}
	public void drawMesh(Canvas canvas){
		drawCoordinate_Y(canvas,30,	baseYStart );
		drawCoordinate_X(canvas,30,baseXStart);
	}

	private void drawCoordinate_X(Canvas canvas,int textSize,int startY) {
		canvas.save();
		float startX = 0;
		float stopX = 700;
		float stopY = startY;
		if(mContext instanceof Activity){
			
			Point point = DevicesUtil.getInstance().getScreenPoint((Activity)mContext);
			stopX = point.x;
			System.out.println(point);
			
		}
		paint.setColor(Color.RED);
		canvas.drawLine(startX, startY, stopX, stopY, paint);
		
		//draw line
		int verSpace = 10;
		for(int i = 0 ; i < stopX ; i+=textSize){
			canvas.drawLine(i,  stopY, i, stopY + verSpace, paint);
		}
		
		Point originalPoint = new Point();
		//draw text
		verSpace += verSpace;
		for(int i = 0 ; i < stopX ; i+=textSize){
			paint.setTextSize(textSize);
			paint.setTextAlign(Align.LEFT);
			originalPoint.set( i , (int) (stopY + verSpace*3));
			if(i < baseYStart || i > baseYStart+maxMarginForY){
				drawVerticalText(canvas, originalPoint, i+"", FontUtil.getTextHeight(paint));
			}
		}
		
		baseXStart = startY;
	}
	
	private void drawCoordinate_Y(Canvas canvas,int textSize,int startX) {
		canvas.save();
		float startY = 0;
		float stopX = startX;
		float stopY = 700;
		if(mContext instanceof Activity){
			
			Point point = DevicesUtil.getInstance().getScreenPoint((Activity)mContext);
			stopY = point.y;
			System.out.println(point);
		}
		
		paint.setColor(Color.RED);
		canvas.drawLine(startX, startY, stopX, stopY, paint);


		//draw line
		int horiSpace = 10;
		for(int i = 0 ; i < stopY ; i+=textSize){
			canvas.drawLine(startX,  i, stopX+horiSpace, i, paint);
		}
		
		//draw text
		horiSpace += horiSpace;
		for(int i = 0 ; i < stopY ; i+=textSize){
			paint.setTextSize(textSize);
			canvas.drawText("" + i, startX + horiSpace, i, paint);
			
			maxMarginForY = FontUtil.getTextWidth(paint, ""+i);
		}
		
	
	}
	
	
	public void drawVerticalText(Canvas canvas,Point originalPoint,String text,int space){
		for(int i = 0;i < text.length(); i++){
			canvas.drawText(text.charAt(i)+"", originalPoint.x,
					originalPoint.y + i*space
					, paint);
		}
	}
	
	/**
	 * 计算两点之间的距离
	 * @param pStart
	 * @param pEnd
	 * @return
	 */
	public static double valueofline(Point pStart, Point pEnd) { // 传入起点和终点的位置坐标
		double result = 0.0; // 结果
		result = Math.sqrt((pEnd.y - pStart.y) * (pEnd.y - pStart.y)
				+ (pEnd.x - pStart.x) * (pEnd.x - pStart.x)); // 平面距离
		return result; // 返回结果
	} 
	
	
}
